<template>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <PartitionTitle title="核心关注" type='line' v-if='device == "desktop"'></PartitionTitle>
        <el-cascader
          v-show='activeName == 2 || activeName == 3'
          v-model="erpCommonList"
          :options="treeList"
          :props='defaultProps'
          ref="all"
          size="small"
          style="width:300px"
          readonly
          @change="selectQuery('all')"
        >
        </el-cascader>

        <!-- 过滤工位 -->
        <el-cascader
          v-show='activeName != 2 && activeName != 3'
          v-model="erpNOstationCommonList"
          :options="treeNostationList"
          :props='defaultProps'
          ref="noStation"
          size="small"
          style="width:300px"
          readonly
          @change="selectQuery('noStation')"
        >
        </el-cascader>
      </div>

      <el-form :inline="true">
        <el-form-item>
          <el-button
            v-for="item in tabList"
            :key="item.key"
            :type="activeName == item.key ? 'primary' : ''"
            size="small"
            class='primary-button-style'
            @click="changeTab(item)"
          >{{item.label}}</el-button>
        </el-form-item>
      </el-form>
      <productOrder v-if='activeName == 1' :lineIds="lineIds"/>
      <deviceStatus v-if='activeName == 2' :queryType="queryType" :queryData="queryIds" ref='status'/>
      <shutdown v-if='activeName == 3' :queryType="queryType" :queryData="queryIds"/>
      <taskAnalysis v-if='activeName == 4' :filterData="queryParmas"/>
      <abnormalAnalysis v-if='activeName == 5' :lindCodes="lindCodes" :filterData="queryParmas"/>
    </el-card>
</template>
<script>
  import {getUnClosedOrder} from "@/api/workbench/index";
  import {statusMap} from "../../exceptionMgmt/enum.js"
  import deviceStatus from "./deviceStatus"
  import shutdown from "./shutdown"
  import taskAnalysis from "./taskAnalysis"
  import abnormalAnalysis from "./abnormalAnalysis"
  import productOrder from "./productOrder"
  import { mapState,mapGetters} from "vuex";
export default {
  props:{
    userInfo:{
      type:Object,
      default:()=>{}
    }
  },
  data() {
    return {
      tabList:[
        {label:"当前生产工单",key:"1"},
        {label:"设备状态",key:"2"},
        {label:"停机情况",key:"3"},
        {label:"任务分析",key:"4"},
        {label:"异常分析",key:"5"}
      ],
      queryParmas:{
        ownedFactory:null,
        ownedWorkshop:null,
        ownedProductionLine:null,
        lineId:null
      },
      activeName:"",
      loading: false,
      lineIds:"",
      lindCodes:"",
      defaultProps: {
        value: "code",
        label: "label",
        checkStrictly: true,
        emitPath: true,
      },
      treeList:[],
      treeNostationList:[],
      erpCommonList:[],
      erpNOstationCommonList:[],
      queryIds:"",
      queryType:"",
    };
  },

  computed: {
    ...mapGetters([
      'treePermitList',
      'allList'
    ]),
    ...mapState({
      device: (state) => state.app.device
    })
  },

  // created(){
  //   this.getList();
  // },

  methods:{
    changeTab(item){
      this.activeName = item.key;
      if(this.activeName == 2){//设备状态
        this.selectQuery("all");
      }
      else if(this.activeName == 3){//停机情况
        this.selectQuery("all");
      }
      else{
        this.selectQuery("noStation");
      }
    },

    getList() {
      if(this.treePermitList?.length){
        this.treeList = this.getTreeData(structuredClone(this.treePermitList));
        const {produceManagerHead,plantManager,workshopHead,lineHead,groupHead,stationBind} = this.userInfo;
        if(produceManagerHead || plantManager || (!workshopHead && !groupHead && !lineHead && !stationBind)){//生产经理,厂长，没有任何岗位
          this.queryType = "line";
          const factoryList = this.treePermitList || [];
          const workShopList = this.allList[0]?.workshopVos?.length ? this.allList[0]?.workshopVos : [];
          const lineIds = [];
          const lindCodes = [];
          this.queryParmas.ownedProductionLine = null;
          this.queryParmas.lineId = null;
          this.queryParmas.ownedWorkshop = null;
          this.queryParmas.ownedFactory = factoryList[0]?.code || null;
          this.erpCommonList = [factoryList[0]?.code || null];
          this.erpNOstationCommonList = [factoryList[0]?.code || null];
          if(workShopList?.length){
            workShopList.forEach(item => {
              if(item.productionLineVos?.length){
                item.productionLineVos.forEach(ele => {
                  lineIds.push(ele?.lineId);
                  lindCodes.push(ele?.lineCode)
                })
              }
            })
            if(lineIds?.length){
              this.queryIds = lineIds.join();
              this.lineIds = lineIds.join();
            }else{
              this.queryIds = "";
              this.lineIds = ""
            }
            if(lindCodes?.length){
              this.lindCodes = lindCodes.join();
            }else{
              this.lindCodes = ''
            }
          }
          else{
            this.queryIds = '';
            this.lindCodes = '';
            this.lineIds = '';
          }
        }
        else if(workshopHead){//车间负责人
          this.queryType = "line";
          const workShopList = this.treePermitList[0]?.children || [];
          const workshopCode = workShopList[0]?.code || null;   //当前显示的车间的编码
          const workshopItem = this.allList[0]?.workshopVos?.length ? this.allList[0]?.workshopVos.find(ele => ele.workshopCode === workshopCode) : {}
          const lineList = workshopItem?.productionLineVos || [];
          if(lineList?.length){
            this.lineIds = lineList.map(ele => ele?.lineId).join();
            this.queryIds = lineList.map(ele => ele?.lineId).join();
            this.lindCodes = lineList.map(ele => ele?.lineCode).join();
          }else{
            this.lineIds = ""
            this.queryIds = ""
            this.lindCodes = ""
          }
          this.queryParmas.ownedProductionLine = null;
          this.queryParmas.lineId = null;
          this.queryParmas.ownedFactory = this.treePermitList[0].code || null;
          this.queryParmas.ownedWorkshop = workShopList[0]?.code || null
          this.erpCommonList = [
            this.treePermitList[0].code || null,
            workShopList[0]?.code || null
          ]
          this.erpNOstationCommonList = [
            this.treePermitList[0].code || null,
            workShopList[0]?.code || null
          ]
        }
        else if(lineHead || groupHead || stationBind){//班组负责人/产线负责人/工位人员
          this.queryType = "line";
          this.queryIds = this.treePermitList[0]?.children[0]?.children[0]?.id || null;
          this.lineIds = this.treePermitList[0]?.children[0]?.children[0]?.id || null;
          this.lindCodes = this.treePermitList[0]?.children[0]?.children[0]?.code || null;
          this.queryParmas.ownedFactory = this.treePermitList[0].code || null;
          this.queryParmas.ownedWorkshop = this.treePermitList[0]?.children[0]?.code || null;
          this.queryParmas.ownedProductionLine = this.treePermitList[0]?.children[0]?.children[0]?.code || null;
          this.queryParmas.lineId = this.treePermitList[0]?.children[0]?.children[0]?.id || null;
          this.erpCommonList = [
            this.treePermitList[0].code || null,
            this.treePermitList[0]?.children[0]?.code || null,
            this.treePermitList[0]?.children[0]?.children[0].code || null
          ]
          this.erpNOstationCommonList = [
            this.treePermitList[0].code || null,
            this.treePermitList[0]?.children[0]?.code || null,
            this.treePermitList[0]?.children[0]?.children[0].code || null
          ]
        }
        // else if(stationBind){//工位人员
        //   this.queryType = "station";
        //   this.queryIds =  this.treePermitList[0]?.children[0]?.children[0]?.children[0]?.id || null;
        //   this.erpCommonList = [
        //     this.treePermitList[0].code || null,
        //     this.treePermitList[0]?.children[0]?.code || null,
        //     this.treePermitList[0]?.children[0]?.children[0].code || null,
        //     this.treePermitList[0]?.children[0]?.children[0]?.children[0]?.code || null
        //   ]
        //   this.erpNOstationCommonList = [
        //     this.treePermitList[0].code || null,
        //     this.treePermitList[0]?.children[0]?.code || null,
        //     this.treePermitList[0]?.children[0]?.children[0].code || null
        //   ]
        // }
        this.treeNostationList = this.treePermitList.map(factory => {
          return{
            code:factory.code,
            id:factory.id,
            label:factory.label,
            children:factory?.children?.length ? factory.children.map(workshop => {
              return{
                code:workshop.code,
                id:workshop.id,
                label:workshop.label,
                workshopType:workshop.workshopType,
                children:workshop?.children?.length ? workshop.children.map(line => {
                  return{
                    code:line.code,
                    id:line.id,
                    label:line.label
                  }
                }):[]
              }
            }):[]
          }
        })
      }
      this.activeName = this.activeName? this.activeName : "1";
    },

    selectQuery(type){
      const item = this.$refs[`${type}`].getCheckedNodes();
      if(item?.length){
        if(item[0].level == 4){//选工位
          this.queryType = "station";
          this.queryIds = item[0]?.data?.id || null;
        }
        else if(item[0].level == 3){//选产线
          this.queryType = "line";
          this.queryIds = item[0]?.data?.id || null;
          this.lineIds = item[0]?.data?.id || null;
          this.lindCodes = item[0]?.data?.code || null;
          this.queryParmas.ownedProductionLine = item[0]?.data?.code || null;
          this.queryParmas.lineId = item[0]?.data?.id || null;
          this.queryParmas.ownedWorkshop = item[0]?.pathNodes[1]?.data?.code || null;
          this.queryParmas.ownedFactory = item[0]?.pathNodes[0]?.data?.code || null;
        }
        else if(item[0].level == 2){//选车间
          this.queryType = "line";
          const workshopCode = item[0]?.data?.code || null;
          const workshopItem = this.allList[0]?.workshopVos?.length ? this.allList[0]?.workshopVos.find(ele => ele.workshopCode === workshopCode) : {}
          const lineList = workshopItem?.productionLineVos || [];
          if(lineList?.length){
            this.queryIds = lineList.map(ele => ele?.lineId).join();
            this.lineIds = lineList.map(ele => ele?.lineId).join();
            this.lindCodes = lineList.map(ele => ele?.lineCode).join();
          }else{
            this.queryIds = "";
            this.lineIds = "";
            this.lindCodes = "";
          }
          this.queryParmas.ownedProductionLine = null;
          this.queryParmas.lineId = null;
          this.queryParmas.ownedFactory = item[0]?.pathNodes[0]?.data?.code || null;;
          this.queryParmas.ownedWorkshop = item[0]?.data?.code || null
        }
        else if(item[0].level == 1){//选工厂
          this.queryType = "line";
          const workShopList = this.allList[0]?.workshopVos?.length ? this.allList[0]?.workshopVos : [];
          this.queryParmas.ownedProductionLine = null;
          this.queryParmas.lineId = null;
          this.queryParmas.ownedWorkshop = null;
          this.queryParmas.ownedFactory = item[0]?.data?.code || null
          const lineIds = [];
          const lindCodes = [];
          if(workShopList?.length){
            workShopList.forEach(item => {
              if(item.productionLineVos?.length){
                item.productionLineVos.forEach(ele => {
                  lineIds.push(ele?.lineId);
                  lindCodes.push(ele?.lineCode)
                })
              }
            })
          }
          if(lineIds?.length){
            this.queryIds = lineIds.join();
            this.lineIds = lineIds.join();
          }
          else{
            this.queryIds = "";
            this.lineIds = "";
          }
          if(lindCodes?.length){
            this.lindCodes = lindCodes.join();
          }else{
            this.lindCodes  = ""
          }
        }
      }else{
        this.queryIds = "";
        this.lineIds = "";
        this.lindCodes  = ""
      }
    }
  },

  watch:{
    userInfo:{
      handler(newVal,oldVal){
        if(newVal?.produceManagerHead != null && newVal?.produceManagerHead != undefined){
          this.getList()
        }
      },
      deep: true,
      immediate: true
    }
  },

  components:{
    deviceStatus,
    productOrder,
    taskAnalysis,
    abnormalAnalysis,
    shutdown
  }

};
</script>
<style lang="scss" scoped>
.box-card{
  width: 100%;
  .clearfix{
    display:flex;
    align-items: center;
    color: #F59A23;
    .block-title{
      margin-bottom: 0;
    }
    .date{
      text-align: right;
      flex: 1;
      span{
        color: #34855B;
        font-size: 14px;
        margin-right: 20px;
      }
    }
  }
}

</style>
